package o;

import android.text.TextUtils;
import com.huawei.appgallery.base.httpskit.ResultField;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class bjv {
    public static <T> T ॱ(String str, Class<T> cls) throws JSONException, IllegalAccessException, InstantiationException {
        T newInstance = cls.newInstance();
        JSONObject jSONObject = new JSONObject(str);
        do {
            Field[] declaredFields = cls.getDeclaredFields();
            if (declaredFields.length > 0) {
                for (final Field field : declaredFields) {
                    if (field.isAnnotationPresent(ResultField.class)) {
                        if (!field.isAccessible()) {
                            AccessController.doPrivileged(new PrivilegedAction() {
                                public Object run() {
                                    field.setAccessible(true);
                                    return null;
                                }
                            });
                        }
                        String value = ((ResultField) field.getAnnotation(ResultField.class)).value();
                        if (TextUtils.isEmpty(value)) {
                            value = field.getName();
                        }
                        Object ˎ = ˎ(jSONObject.optString(value), field.getGenericType());
                        if (ˎ != null) {
                            field.set(newInstance, ˎ);
                        }
                    }
                }
            }
            cls = cls.getSuperclass();
        } while (cls != Object.class);
        return newInstance;
    }

    private static Object ˎ(String str, Type type) throws IllegalAccessException, JSONException, InstantiationException {
        int i = 0;
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        Type ˎ = bju.ˎ(type);
        Object ʽ;
        Type type2;
        if (ॱॱ(ˎ)) {
            ʽ = ʽ(ˎ);
            type2 = ((ParameterizedType) type).getActualTypeArguments()[0];
            JSONArray jSONArray = new JSONArray(str);
            int length = jSONArray.length();
            for (int i2 = 0; i2 < length; i2++) {
                ((Collection) ʽ).add(ˎ(jSONArray.optString(i2), type2));
            }
            return ʽ;
        } else if (ʼ(ˎ)) {
            Object ʽ2 = ʽ(ˎ);
            type2 = ((ParameterizedType) type).getActualTypeArguments()[1];
            JSONObject jSONObject = new JSONObject(str);
            Iterator keys = jSONObject.keys();
            while (keys.hasNext()) {
                String str2 = (String) keys.next();
                ((Map) ʽ2).put(str2, ˎ(jSONObject.optString(str2), type2));
            }
            return ʽ2;
        } else if (!ˎ.isArray()) {
            return ˊ(str, ˎ);
        } else {
            Type componentType = ˎ.getComponentType();
            JSONArray jSONArray2 = new JSONArray(str);
            int length2 = jSONArray2.length();
            ʽ = Array.newInstance(componentType, length2);
            while (i < length2) {
                Array.set(ʽ, i, ˎ(jSONArray2.optString(i), componentType));
                i++;
            }
            return ʽ;
        }
    }

    private static boolean ˎ(String str) {
        return HwAccountConstants.NULL.equals(str) || TextUtils.isEmpty(str);
    }

    private static Object ˊ(String str, Type type) throws IllegalAccessException, JSONException, InstantiationException {
        Class ˎ = bju.ˎ(type);
        if (ˏ(ˎ)) {
            try {
                return Integer.valueOf(Integer.parseInt(str));
            } catch (NumberFormatException e) {
                return Integer.valueOf(0);
            }
        } else if (ˊ(ˎ)) {
            try {
                return Double.valueOf(Double.parseDouble(str));
            } catch (NumberFormatException e2) {
                return Double.valueOf(0.0d);
            }
        } else if (ˋ(ˎ)) {
            return Boolean.valueOf(Boolean.parseBoolean(str));
        } else {
            if (ॱ(ˎ)) {
                try {
                    return Long.valueOf(Long.parseLong(str));
                } catch (NumberFormatException e3) {
                    return Long.valueOf(0);
                }
            } else if (ˎ(ˎ)) {
                return ˎ(str) ? "" : String.valueOf(str);
            } else {
                return ॱ(str, ˎ);
            }
        }
    }

    private static boolean ˎ(Class<?> cls) {
        return cls == String.class;
    }

    private static boolean ॱ(Class<?> cls) {
        return cls == Long.TYPE || cls == Long.class;
    }

    private static boolean ˋ(Class<?> cls) {
        return cls == Boolean.TYPE || cls == Boolean.class;
    }

    private static boolean ˊ(Class<?> cls) {
        return cls == Double.TYPE || cls == Double.class || cls == Float.TYPE || cls == Float.class;
    }

    private static boolean ˏ(Class<?> cls) {
        return cls == Integer.TYPE || cls == Integer.class;
    }

    private static boolean ॱॱ(Class<?> cls) {
        return Collection.class.isAssignableFrom(cls);
    }

    private static boolean ʼ(Class<?> cls) {
        return Map.class.isAssignableFrom(cls);
    }

    private static Object ʽ(Class<?> cls) throws IllegalAccessException, InstantiationException, JSONException {
        if (!cls.isInterface()) {
            return cls.newInstance();
        }
        if (cls.equals(List.class)) {
            return ArrayList.class.newInstance();
        }
        if (cls.equals(Set.class)) {
            return HashSet.class.newInstance();
        }
        if (cls.equals(Map.class)) {
            return HashMap.class.newInstance();
        }
        throw new JSONException("the type of " + cls + "cannot be interface");
    }
}
